package com.datastructure.test.levelofiterator;

import java.util.ArrayList;

/**
 * 二叉树层序遍历
 */
public class LevelOfIterator {
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        // write code here
        if(root == null){
            return res;
        }
        count(root,0);
        return res;
    }

    public void count(TreeNode node, int level){
        if(level == res.size()){
            res.add(new ArrayList<Integer>());
        }

        ArrayList<Integer> list = res.get(level);
        list.add(node.val);

        if(node.left != null){
            count(node.left, level+1);
        }

        if(node.right != null){
            count(node.right, level+1);
        }

    }
}
class TreeNode {
   int val = 0;
   TreeNode left = null;
    TreeNode right = null;
}
